﻿Namespace Validation
    Public Class StringLengthValidationRule
        Inherits WPFGlue.Validation.ValidationRule

        Public Property MaxLength As Integer

        Public Sub New()
            Me.ValidationStep = System.Windows.Controls.ValidationStep.RawProposedValue
            Me.Message = "Must not have more than {1} characters."
        End Sub

        Protected Overrides Function ValidateCore(ByVal value As Object, ByVal cultureInfo As System.Globalization.CultureInfo) As Boolean
            Dim result As Boolean = (MaxLength = 0) OrElse value.ToString.Length <= MaxLength
            Return result
        End Function

        Protected Overrides Function FormatMessage(ByVal value As Object) As String
            Return String.Format(Me.Message, value, MaxLength)
        End Function
    End Class
End Namespace